package net.heavencraft.bejeweled.util;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

import net.heavencraft.bejeweled.jewel.CreeperJewel;
import net.heavencraft.bejeweled.jewel.EnderJewel;
import net.heavencraft.bejeweled.jewel.Jewel;
import net.heavencraft.bejeweled.jewel.JewelColor;
import net.heavencraft.bejeweled.jewel.SkeletonJewel;
import processing.core.PApplet;
import processing.core.PImage;

/**
 * Provides images for Jewels Must be initialized
 * 
 * @author Sven
 * 
 */
public class ImageProvider {

	/**
	 * Contains the images the jewels
	 */
	private Map<String, PImage> images = new HashMap<>();

	/**
	 * Contains the images of the skeleton jewels
	 */
	private Map<String, PImage> imagesSkeleton = new HashMap<>();

	/**
	 * Contains the images of the creeper jewels
	 */
	private Map<String, PImage> imagesCreeper = new HashMap<>();

	/**
	 * Contains the image of the ender jewel
	 */
	private PImage enderImage;

	/**
	 * Initializes a new ImageProvider that can contains the textures for the
	 * jewels
	 * 
	 * @param main
	 */
	public ImageProvider(PApplet main) {
		EnumSet<JewelColor> types = EnumSet.allOf(JewelColor.class);
		for (JewelColor color : types) {
			images.put(color.toString(), main.loadImage(color.getName()));
			imagesSkeleton.put(color.toString(),
					main.loadImage("skeleton_" + color.getName()));
			imagesCreeper.put(color.toString(),
					main.loadImage("creeper_" + color.getName()));
			enderImage = main.loadImage("skull_enderman.png");
		}
		System.out.println("Successfully loaded all gem textures");
	}

	/**
	 * Gets the image of a JewelColor
	 * 
	 * @param color
	 *            The color for the Jewel
	 * @return The image for this JewelColor
	 */
	public PImage getImage(JewelColor color) {
		return images.get(color.toString());
	}

	/**
	 * Gets the image of this Jewel
	 * 
	 * @param jewel
	 *            The jewel to obtain the image from
	 * @return The image of this Jewel
	 */
	public PImage getImage(Jewel jewel) {
		if (jewel instanceof SkeletonJewel) {
			return imagesSkeleton.get(jewel.getColor().toString());
		} else if (jewel instanceof EnderJewel) {
			return enderImage;
		} else if (jewel instanceof CreeperJewel) {
			return imagesCreeper.get(jewel.getColor().toString());
		} else {
			return images.get(jewel.getColor().toString());
		}
	}
}
